//初始化数据库,Sequelize提供了一个sync()方法，可以自动创建数据库。直接在这里初始化，使用sequelize的api
const models = require('./model');
const CryptoUtil = require('./src/main/utils/CryptoUtil');

models.sync({force:true}).then(() => {
  console.log('sync done,db inited');
}).then(()=>{
  if(models.User){
    console.log("存在用户model user信息")
    let User = models.User;
    let cryptoinfo = CryptoUtil.crypto(CryptoUtil.cryptoWithoutSalt("123456"));
    return User.create({
      "email": "191550636@qq.com",
      "pwd": cryptoinfo[0],
      "salt":cryptoinfo[1],
      "nickname":"zzq123",
      "sex":"0",
      "recipient":"张智泉",
      "address":"北京市海淀区",
      "phone":"18500944413",
      "headimg":"http://tvax4.sinaimg.cn/crop.0.0.480.480.180/768c39d5ly8fjje1d0teej20dc0dcq35.jpg"
    })
  }
})
.then((resp)=>{
    console.log("新增完成")
    //更新查看更新时间和version
    console.log("============update=====================")
    // console.log(resp)
    return resp.update(
        {'nickname':'zhangzq'}
    )
  })
.then((resp)=>{
  console.log("更新用户完成，")
})
.then(()=>{
  if(models.Admin){
    console.log("存在用户model admin信息")
    let Admin = models.Admin;
    let cryptoinfo = CryptoUtil.crypto(CryptoUtil.cryptoWithoutSalt("123456"));
    return Admin.create({
      account: 'admin',
      pwd: cryptoinfo[0],
      salt: cryptoinfo[1],
      name:'zzq',
    })
  }
})
.then(()=>{
  console.log("新增admin用户！")
})
.then(()=>{
  if(models.Type && models.Goods && models.GoodsDetail){
    console.log("存在用户model type信息");
    let Type = models.Type;
    let Goods = models.Goods;
    let GoodsDetail = models.GoodsDetail;
    let type1 =  {
      id:models.generateId(),
      name:'春季花种',
    };
    let goods1 ={
      id:models.generateId(),
      "name":"树叶子",
      "typeId":type1.id,
      img:"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcTsvorexxKJgtT83Lefkb1xp0iOBZpAbzYRGjI6w19NMGPj4Bh8",
      desc:"树叶子啊！"
    };
    let goods2 ={
      id:models.generateId(),
      "name":"老花",
      "typeId":type1.id,
      img:"https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSVqDWTryjC6GWnbGGXblfkg_H8C2vsU-MWnFUb-08DJE5MrFPE",
      desc:"每个花都是好的，真的好的！"
    };
    Type.create(type1);
    Goods.create(goods1);
    Goods.create(goods2);
    //goods的规格描述
    GoodsDetail.create({
      "goodsId":goods1.id,
      "specName":"默认goods1",
      "stockNum":20,
      "unitPrice":10
    });
    //goods2两种规格
    GoodsDetail.create({
      "goodsId":goods2.id,
      "specName":"月季goods2",
      "stockNum":123,
      "unitPrice":1
    })
    return GoodsDetail.create({
      "goodsId":goods2.id,
      "specName":"月季规格2",
      "stockNum":12,
      "unitPrice":3
    })
    
  }
})
.then((resp) => {
  let gd2 = resp.dataValues;
   console.log(gd2)
   console.log("==================================")
})
.then(resp => {

  const Type = models.Type;
  return Type.create({
    "id":models.generateId(),
    name:'夏季花种',
  })
})
.then(resp => {
  if(models.Goods && models.GoodsDetail){
    console.log("存在用户model type信息");
    let Goods = models.Goods;
    let GoodsDetail = models.GoodsDetail;
    let type2 = resp.dataValues;
    let goods3 ={
      id:models.generateId(),
      "name":"春季花",
      "typeId":type2.id,
      img:"http://img02.tooopen.com/images/20160509/tooopen_sy_161967094653.jpg",
      desc:"春季的花儿"
    };
    let goods4 ={
      id:models.generateId(),
      "name":"喇叭花",
      "typeId":type2.id,
      img:"https://pub-static.haozhaopian.net/static/web/site/features/cn/crop/images/crop_20a7dc7fbd29d679b456fa0f77bd9525d.jpg",
      desc:"喇叭花，真好看，开在春季！"
    };
    Goods.create(goods3);
    Goods.create(goods4);
    //goods的规格描述
    GoodsDetail.create({
      "goodsId":goods3.id,
      "specName":"默认goods3",
      "stockNum":12,
      "unitPrice":1
    });
    return GoodsDetail.create({
      "goodsId":goods4.id,
      "specName":"月季规格4",
      "stockNum":9,
      "unitPrice":3
    })

  }
})
.then(resp => {
    console.log(resp)
    let RebateGoods = models.RebateGoods;
    let gds = [
        {"rebateGoodId":"535604402430","name":"轻薄羽绒服女短款立领连帽时尚韩版修身薄款女款外套大码秋冬反季","mainImg":"http://img.alicdn.com/bao/uploaded/i4/1867587762/O1CN0127D2jYJOmxrBkXE_!!0-item_pic.jpg","detailUrl":"http://item.taobao.com/item.htm?id=535604402430","shopName":"卡西玫旗舰店","price":"279.00","sales":"31019","rate":"4.50","commission":"12.56","salerWangwang":"卡西玫旗舰店","tbkShortLink":"https://s.click.taobao.com/v2jBfKw","tbkLink":"https://s.click.taobao.com/t?e=m%3D2%26s%3DB9lSCQ12AsMcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67g0E%2FAR4Hai2J%2BAVY%2F4wKC37OOhWhB7baxHrNB3U9n8U2jHYdudiialngo5wnD1cUBlD4rcsPguaJPj6wHVmSJuja06RtHRAD0cgypuni7XE5irSgXg3Kyo%3D","code":"￥asasbiPDQjf￥","couponCapacity":"50000","couponRest":"44260","couponAmount":"满201元减200元","startTime":"2018-10-31","endTime":"2018-11-10","couponLink":"https://uland.taobao.com/coupon/edetail?e=VHDzwFL16ZZt3vqbdXnGlq42GVYxbLz2%2FBabG3TXUb0LXvUwWfnmnLpJvAGxAOJBHv69%2F64THFBnNNyPkxq6qtVdpYWk2XHK2IaVVct%2FH6faUdFGdLbQtQ%3D%3D&af=1&pid=mm_242780015_177050414_51198350024","couponCode":"￥Im37biPDPZI￥","couponShortLink":"https://s.click.taobao.com/qKmBfKw","isMarketPlan":"否"},
        {"rebateGoodId":"566210433382","name":"2018新款羽绒服女时尚韩版百搭轻薄短款修身显瘦连帽大码冬装外套","mainImg":"http://img.alicdn.com/bao/uploaded/i2/1714125670/O1CN011rku9l5IEDuay67_!!0-item_pic.jpg","detailUrl":"http://item.taobao.com/item.htm?id=566210433382","shopName":"哥恋旗舰店","price":"169.00","sales":"8766","rate":"7.65","commission":"12.93","salerWangwang":"哥恋旗舰店","tbkShortLink":"https://s.click.taobao.com/jUgBfKw","tbkLink":"https://s.click.taobao.com/t?e=m%3D2%26s%3D1xu%2BvvJrNSQcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67g0E%2FAR4Hai2J%2BAVY%2F4wKC37OOhWhB7baxHrNB3U9n8U2jHYdudiialngo5wnD1cUBlD4rcsPgua8aGxUijB7F1%2Bg1iofn3ukIX4tjEv8wV8VskvM2hR8LNxKmPmpIKZsA%3D%3D","code":"￥wu0TbiPD87V￥","couponCapacity":"88888","couponRest":"88888","couponAmount":"满159元减80元","startTime":"2018-11-2","endTime":"2018-11-06","couponLink":"https://uland.taobao.com/coupon/edetail?e=dKMcMO44JnFt3vqbdXnGlq42GVYxbLz2%2FBabG3TXUb0LXvUwWfnmnLpJvAGxAOJBg8pBHJnzzOiOF2twYIdfBNFKE5I2gZF72IaVVct%2FH6e%2FbDB7vEbF3Q%3D%3D&af=1&pid=mm_242780015_177050414_51198350024","couponCode":"￥zIs9biPDZsK￥","couponShortLink":"https://s.click.taobao.com/FEKjrKw","isMarketPlan":"是"},
        {"rebateGoodId":"572188888059","name":"冬装羽绒棉衣服女2018新款韩版宽松bf学生冬季外套ins面包服冬衣","mainImg":"http://img.alicdn.com/bao/uploaded/i3/1072087573/TB2w6NqGACWBuNjy0FaXXXUlXXa_!!1072087573-0-item_pic.jpg","detailUrl":"http://item.taobao.com/item.htm?id=572188888059","shopName":"karoesiy旗舰店","price":"179.00","sales":"2422","rate":"12.00","commission":"21.48","salerWangwang":"karoesiy旗舰店","tbkShortLink":"https://s.click.taobao.com/jXjBfKw","tbkLink":"https://s.click.taobao.com/t?e=m%3D2%26s%3DFUBhe8bUM%2BAcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67g0E%2FAR4Hai2J%2BAVY%2F4wKC37OOhWhB7baxHrNB3U9n8U2jHYdudiialngo5wnD1cUBlD4rcsPguas1N7oCqs%2BGE38DWOVi5ILq9SW2m8NaiGqsxGQRnsKx1xKmPmpIKZsA%3D%3D","code":"￥ira4biPDFHF￥","couponCapacity":"0","couponRest":"0","couponAmount":"无","startTime":"","endTime":"","couponLink":"","couponCode":"","couponShortLink":"","isMarketPlan":"是"},
        {"rebateGoodId":"573177788393","name":"2018秋冬新款立领轻薄羽绒服女短款薄款连帽大码轻便显瘦外套正品","mainImg":"http://img.alicdn.com/bao/uploaded/i3/3883556740/O1CN011zexx4N4xh7Oe3z_!!0-item_pic.jpg","detailUrl":"http://item.taobao.com/item.htm?id=573177788393","shopName":"羽系旗舰店","price":"135.00","sales":"12088","rate":"7.20","commission":"9.72","salerWangwang":"羽系旗舰店","tbkShortLink":"https://s.click.taobao.com/gDfBfKw","tbkLink":"https://s.click.taobao.com/t?e=m%3D2%26s%3DREUaURKCWLQcQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67g0E%2FAR4Hai2J%2BAVY%2F4wKC37OOhWhB7baxHrNB3U9n8U2jHYdudiialngo5wnD1cUBlD4rcsPguaMfJG04bt3%2FFxNPwBw17ts9KQyjFidJfPjKLDGGAXreY%3D","code":"￥10xQbiPDh3b￥","couponCapacity":"100000","couponRest":"55000","couponAmount":"满109元减50元","startTime":"2018-10-13","endTime":"2018-11-06","couponLink":"https://uland.taobao.com/coupon/edetail?e=DXgLGA6CkqJt3vqbdXnGlq42GVYxbLz2%2FBabG3TXUb0LXvUwWfnmnLpJvAGxAOJBB2di0CtOxamM0Q8P8M1O1emG6wVR9PmE2IaVVct%2FH6faUdFGdLbQtQ%3D%3D&af=1&pid=mm_242780015_177050414_51198350024","couponCode":"￥OGi4biPDWgC￥","couponShortLink":"https://s.click.taobao.com/15PjrKw","isMarketPlan":"否"},
        {"rebateGoodId":"574321202514","name":"冬季轻薄双面穿羽绒服女中长款2018新款韩版潮修身白鸭绒过膝外套","mainImg":"http://img.alicdn.com/bao/uploaded/i1/2894992549/O1CN011UhU2mRQWHbyXCa_!!0-item_pic.jpg","detailUrl":"http://item.taobao.com/item.htm?id=574321202514","shopName":"呼啸衫庄旗舰店","price":"399.00","sales":"4865","rate":"9.00","commission":"35.91","salerWangwang":"呼啸衫庄旗舰店","tbkShortLink":"https://s.click.taobao.com/4XiBfKw","tbkLink":"https://s.click.taobao.com/t?e=m%3D2%26s%3DG6LBAqiXv6ccQipKwQzePOeEDrYVVa64K7Vc7tFgwiHjf2vlNIV67g0E%2FAR4Hai2J%2BAVY%2F4wKC37OOhWhB7baxHrNB3U9n8U2jHYdudiialngo5wnD1cUBlD4rcsPguaiui4uLfQDgg9BlgiSECeoydOroYwFqr54UnbiIXUGSJxKmPmpIKZsA%3D%3D","code":"￥podTbiPxYuL￥","couponCapacity":"30000","couponRest":"24600","couponAmount":"满399元减200元","startTime":"2018-11-01","endTime":"2018-11-05","couponLink":"https://uland.taobao.com/coupon/edetail?e=b8x%2BhEJzr%2Fpt3vqbdXnGlq42GVYxbLz2%2FBabG3TXUb0LXvUwWfnmnLpJvAGxAOJBB2di0CtOxalvNsN%2FGoicmNGA9FK9Zbsa2IaVVct%2FH6e%2FbDB7vEbF3Q%3D%3D&af=1&pid=mm_242780015_177050414_51198350024","couponCode":"￥uisAbiPDeLq￥","couponShortLink":"https://s.click.taobao.com/zPFjrKw","isMarketPlan":"是"},
        {"rebateGoodId":"578491135494","name":"花花公子贵宾冬季新款棉衣流男修身连帽加厚外套短款韩版潮流男装","mainImg":"http://img.alicdn.com/bao/uploaded/i2/2200316157/O1CN011vLx2khzIYPu1yQ_!!2200316157.jpg","detailUrl":"http://item.taobao.com/item.htm?id=578491135494","shopName":"花花公子贵宾衣神店","price":"398.00","sales":"363","rate":"18.00","commission":"71.64","salerWangwang":"衣神前传199010","tbkShortLink":"https://s.click.taobao.com/jpmBfKw","tbkLink":"https://s.click.taobao.com/t?e=m%3D2%26s%3DJHr3LkAMmZYcQipKwQzePOeEDrYVVa64LKpWJ%2Bin0XLjf2vlNIV67g0E%2FAR4Hai2J%2BAVY%2F4wKC37OOhWhB7baxHrNB3U9n8U2jHYdudiialngo5wnD1cUBlD4rcsPgua9XyngxI8MnKuVJS6njayBNkT8u0rag%2BRbJBdwKCoumU%3D","code":"￥3H8RbiPDbF7￥","couponCapacity":"50000","couponRest":"50000","couponAmount":"满320元减300元","startTime":"2018-11-02","endTime":"2018-11-09","couponLink":"https://uland.taobao.com/coupon/edetail?e=cB1xYnwykm9t3vqbdXnGlq42GVYxbLz2%2FBabG3TXUb0LXvUwWfnmnLpJvAGxAOJBB2di0CtOxanw7d8BIazarPmMvTUZaaQT2IaVVct%2FH6faUdFGdLbQtQ%3D%3D&af=1&pid=mm_242780015_177050414_51198350024","couponCode":"￥ID0FbiPDq3I￥","couponShortLink":"https://s.click.taobao.com/fwdBfKw","isMarketPlan":"否"},
    ];

    gds.forEach(gd => {
      gd["id"] = models.generateId();
      gd["category"] = "折扣";
      gd["parentCategory"] = "促销";
      gd["type"] = "衣物";
    });

    for(var i =0,len = gds.length -1; i<len;i++){
        RebateGoods.create(gds[i]).catch(e => {
            console.log(e)
        });
    }

    return RebateGoods.create(gds[gds.length - 1]).catch(e => {
        console.log(e)
    });
})
.then((rsp)=>{
  console.log("resp:::"+rsp)
})
.then(() => {
  console.log("type 信息更新完成！")
})
.catch((e) => {
  console.log(`failed:${e}`);
})
.finally(() =>{
  process.exit(0);
});

//准备数据user
function init_data(){

  console.log("执行数据初始化！")
  if(models.User){
    console.log("存在用户model user信息")
    let User = models.User;
    let user = User.create({
      "email": "191550636@qq.com",
      "pwd": "123456",
      "nickname":"zzq123",
      "sex":"0",
      "recipient":"张智泉",
      "address":"北京市海淀区",
      "phone":"18500944413",
      "headimg":"http://tvax4.sinaimg.cn/crop.0.0.480.480.180/768c39d5ly8fjje1d0teej20dc0dcq35.jpg"
    }).then(resp => {
      user= resp;
      console.log("新增完成")
      return user;
    }).then(resp =>{
      //更新查看更新时间和version
      console.log("============update=====================")
      // console.log(resp)
      resp.update(
          {'nickname':'zhangzq'}
      )
    })
    .then(resp => {
      console.log("更新用户完成，")
    })
    .catch(e => {
      console.log(e)
    });
  }

  if(models.Admin){
    console.log("存在用户model admin信息")
    let Admin = models.Admin;
    const admin = Admin.create({
      account: 'admin',
      pwd: '123456',
      name:'zzq',
    })
    .catch(e => {
      console.log(e)
    });
  }

  if(models.Type){
    console.log("存在用户model type信息")
    let Type = models.Type;
    const type1 = Type.create({
      "id":models.generateId(),
      name:'春季花',
    })
    .catch(e => {
      console.log(e)
    });
    const type2 = Type.create({
      "id":models.generateId(),
      name:'夏季花',
    })
    .catch(e => {
      console.log(e)
    });
  }
  if(models.RebateGoods){
      console.log("存在用户model rebate goods信息")

  }
}

//这句是一开始没有返回promise对象的时候使用回调来处理初始化数据
// models.sync(init_data);


